namespace :choice do
  # rake choice:auto_add_choice
  desc "auto_add_choice"
  task auto_add_choice: :environment do
    puts "【choice:auto_add_choice】#{Time.now} start"
    block_flows = BlockFlow.where(cate_type: "N", mark: "base").last(3)
    block_flows.each do |block_flow|
      block = Block.find_by_name block_flow.name
      trade_date_cycle = trade_date(10)
      stock_klines = Parallel.map(block.stocks, in_processes: 8, progress: true) do |stock|
        kline = Tushare::Stock::Trading.get_k_data(stock.symbol, "#{trade_date_cycle[0..3]}-#{trade_date_cycle[4..5]}-#{trade_date_cycle[6..7]}", Time.now.strftime("%Y-%m-%d"))
        next if kline.empty?
        realtime_stock = JSON.parse $Redis.get(stock.symbol)
        ma_5 = (kline.pluck("close").last(4).sum.to_f + realtime_stock["trade"].to_f) / 5
        { name: stock.name, symbol: stock.symbol, close: realtime_stock["trade"].to_f , ma_5: ma_5, ths_flow: stock.ths_flow, year_count: stock.year_count, is_buy_point: stock.is_buy_point, is_choice: stock.is_choice, amplitude: cul_change(kline.pluck("low").min, kline.pluck("high").max) }
      end
      amplitude_top1 = stock_klines.compact.sort_by { |x| x[:amplitude] }.reverse.first
      stock = Stock.find_by_symbol(amplitude_top1[:symbol]) if amplitude_top1.present?

      Robot.send_alert "#{block.name} #{stock.name} -- #{stock.symbol} 被添加到自选股"
      if amplitude_top1[:close] > amplitude_top1[:ma_5]
        stock.update(is_choice: true, ma_alert: -5)
        stock.update(sell_price: (amplitude_top1[:close] * 1.03).round(2)) if stock.buy_price.blank?
        stock.update(choice_block_tag: block.name)
      else
        stock.update(is_choice: true, ma_alert: 5)
        stock.update(buy_price: (amplitude_top1[:close] * 0.97).round(2)) if stock.buy_price.blank?
        stock.update(choice_block_tag: block.name)
      end
    end
    puts "【choice:auto_add_choice】#{Time.now} end"
  end

  # 一段时间内振幅大于50% 今天收盘价格低于一段时间内最高点30%
  # rake choice:amplitude_choice
  desc "amplitude_choice"
  task :amplitude_choice, [:start_end_date] => :environment do |t, args|
    puts "【choice:amplitude_choice】#{Time.now} start"
    start_end_date = args[:start_end_date]
    start_date = start_end_date.split("-").first
    end_date = start_end_date.split("-").last
    puts start_date, end_date
    Stock.all.each do |stock|
      dailies = $TusharePro.tushare_pro("daily", ts_code: stock.ts_code, start_date: start_date, end_date: end_date)
      next if dailies.blank?
      high_price = dailies.pluck("high").max
      low_price = dailies.pluck("low").min
      close_price = dailies.last["close"].to_f
      amplitude = cul_change(low_price, high_price)
      delay_millisecond(1, 200){}
      if amplitude >= 50 && close_price <= (high_price * 0.7)
        close = Tushare::Stock::Trading.get_k_data(stock.symbol).last["close"]
        high_close_amplitude = cul_change(high_price, close)
        if high_close_amplitude < -25
          puts "#{stock.name} -- #{stock.symbol} 符合振幅选股条件，振幅：#{amplitude}%，最高价：#{high_price}，当前价：#{close_price}，收盘价距离最高点跌幅：#{high_close_amplitude}%"
        end
      end
    end
  end
end